/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.assoUdt;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.InterfaceGenerator;
import com.inspur.edp.caf.generator.item.InterfaceGeneratorContext;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.AssoUdtItemPropertyGenerator;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.util.ArrayList;
import java.util.Arrays;
import lombok.var;
import org.eclipse.jdt.core.dom.Modifier;

public class AssoUdtEnrichedDataGenerator extends InterfaceGenerator {

  private IGspCommonField field;
  private SimpleDataTypeDef udt;
  private DataTypeDataGeneratorContext parentDataGenCtx;

  public AssoUdtEnrichedDataGenerator(
      IGspCommonField field, SimpleDataTypeDef udt, DataTypeDataGeneratorContext dataGenContext) {
    super(dataGenContext.getDataTypeGeneratorContext().getEntityGenContext()
        .getCompilationUnitInfo());
    this.field = field;
    this.udt = udt;
    this.parentDataGenCtx = dataGenContext;
  }

  @Override
  protected InterfaceGeneratorContext createInterfaceItemContext() {
    DataTypeDataGeneratorContext ctx = new DataTypeDataGeneratorContext(
        parentDataGenCtx.getDataTypeGeneratorContext());
    parentDataGenCtx.getDataTypeGeneratorContext().getEntityGenContext()
        .getDataTypeDataGenContexts().put(field.getID(), ctx);
    return ctx;
  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    getContext().getEnrichedTypes().put(field.getLabelID(), new MediateType(
        field.getBelongObject().getGeneratedEntityClassInfo().getClassNamespace(), getName()));
  }

  @Override
  protected DataTypeDataGeneratorContext getContext() {
    return (DataTypeDataGeneratorContext) super.getContext();
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
    return new ArrayList(
        Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected String getName() {
    return getItemName(field);
  }

  private static final String nameSuffix = "";

  public static String getFullItemName(IGspCommonField field) {
    String className = getItemName(field);
    if (field.getBelongObject() != null) {
      className = field.getBelongObject().getGeneratedEntityClassInfo().getClassNamespace()
          .concat(".").concat(className);
    }
    return className;
  }

  public static String getItemName(IGspCommonField field) {
    return "I".concat(
        field.getBelongObject() != null ? field.getBelongObject().getCode() : "")
        .concat(field.getLabelID());
  }

  @Override
  protected ArrayList<TypeInfo> addParentInterfaces() {
    String packageName =
        udt.getGeneratedEntityClassInfo().getAssemblyInfo().getAssemblyName();

    String className = udt.getGeneratedEntityClassInfo().getClassName();

    return Utils.getUdtPropertyTypeList(
        udt, packageName, className,
        getContext().getDataTypeGeneratorContext().getEntityGenContext().getCefGenContext());

    //   return new List<Type>
    //   {
    //	ReflectionUtil.GetTypeFromAssembly(
    //		udt.getGeneratedEntityClassInfo().AssemblyInfo.getAssemblyName(),
    //		udt.getGeneratedEntityClassInfo().AssemblyInfo.getAssemblyName() + "." +
    // udt.getGeneratedEntityClassInfo().ClassName)
    // };
  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {
    ArrayList<BaseGenerator> rez = new ArrayList<BaseGenerator>();

    for (var refElement : field.getChildAssociations().get(0).getRefElementCollection()) {
      if (refElement.getIsFromAssoUdt()) {
        continue;
      }

      rez.add(
          new AssoUdtItemPropertyGenerator(field.getLabelID(), refElement, getContext()));
    }
    rez.add(new AssoUdtInfoPropertyGenerator(field, udt, getContext()));
    if (Character.isLowerCase(field.getLabelID().charAt(0))) {
      rez.add(new AssoUdtInfoPropertyGenerator(field, udt, getContext(), true));
    }
    return rez;
  }
}
